package org.skyemoon.hbase.hbasetest.controller;

import com.opencsv.exceptions.CsvException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.skyemoon.hbase.hbasetest.convention.Result;
import org.skyemoon.hbase.hbasetest.convention.Results;
import org.skyemoon.hbase.hbasetest.dao.entity.ElectronicDO;
import org.skyemoon.hbase.hbasetest.dao.entity.RadioDO;
import org.skyemoon.hbase.hbasetest.dto.domain.FlightImageDTO;
import org.skyemoon.hbase.hbasetest.dto.req.FlightImageReqDTO;
import org.skyemoon.hbase.hbasetest.dto.domain.ImageDTO;
import org.skyemoon.hbase.hbasetest.dto.resp.AllDataRespDTO;
import org.skyemoon.hbase.hbasetest.dto.resp.FlightRespDTO;
import org.skyemoon.hbase.hbasetest.service.ElectronicService;
import org.skyemoon.hbase.hbasetest.service.HbaseService;
import org.skyemoon.hbase.hbasetest.service.RadioService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import static org.skyemoon.hbase.hbasetest.toolkit.FileUtil.listFileAndWriteToSequenceFile;
import static org.skyemoon.hbase.hbasetest.toolkit.ReadCSV.*;

/**
 * Hbase控制层
 */
@RestController
@RequiredArgsConstructor
@Tag(name = "数据组接口", description = "TZFJ data store test")
public class HbaseController {

    private final HbaseService hbaseService;
    private final RadioService radioService;
    private final ElectronicService electronicService;

    @PostMapping("/api/save")
    @Operation(summary = "all data save")
    public Result<Void> saveFlightImage(@RequestBody FlightImageReqDTO requestParam) throws IOException, CsvException, ParseException {
        System.out.println("-----写入接口开始-----");
        long start_c = System.currentTimeMillis();
        List<ImageDTO> imageReqDTOList = listFileAndWriteToSequenceFile(requestParam.getPath());
        FlightImageDTO flightImageDTO = FlightImageDTO.builder()
                .flightType(requestParam.getFlightType())
                .flightTime(requestParam.getFlightTime())
                .flightNum(requestParam.getFlightNum())
                .flightTask(requestParam.getFlightTask())
                .imageList(imageReqDTOList)
                .build();
        hbaseService.saveInfo(flightImageDTO);

        List<RadioDO> radioDOList = readRadioCSV(requestParam.getRadioPath());
        radioDOList.forEach(each -> {
            each.setFlightType(requestParam.getFlightType());
            each.setFlightNum(requestParam.getFlightNum());
            each.setFlightTask(requestParam.getFlightTask());
            each.setFlightTime(requestParam.getFlightTime());
        });
        radioService.saveRadioData(radioDOList);

        List<ElectronicDO> electronicDOList = readElectronicCSV(requestParam.getElectronicPath());
        electronicDOList.forEach(each -> {
            each.setFlightType(requestParam.getFlightType());
            each.setFlightNum(requestParam.getFlightNum());
            each.setFlightTask(requestParam.getFlightTask());
            each.setFlightTime(requestParam.getFlightTime());
        });
        electronicService.saveElectronicData(electronicDOList);

        long stop_c = System.currentTimeMillis();
        System.out.println("-----写入接口调用时间:"+(stop_c - start_c)*1.0/1000 + "s-----");
        return Results.success();
    }

    @PostMapping("/api/query")
    @Operation(summary = "all data query")
    public Result<AllDataRespDTO> queryByInfo(@RequestBody FlightImageReqDTO requestParam) {
        System.out.println("-----查询接口开始-----");
        long start_c = System.currentTimeMillis();
        List<FlightRespDTO> imageList = hbaseService.queryImageByInfo(requestParam);

        List<RadioDO> radioDOList = radioService.selectRadioData(requestParam);

        List<ElectronicDO> electronicDOList = electronicService.selectElectronicData(requestParam);

        AllDataRespDTO result = AllDataRespDTO.builder()
                .flightRespDTOList(imageList)
                .radioDOList(radioDOList)
                .electronicDOList(electronicDOList)
                .build();

        long stop_c = System.currentTimeMillis();
        System.out.println("-----查询接口调用时间:"+(stop_c - start_c)*1.0/1000 + "s-----");
        return Results.success(result);
    }
}
